package com.youdianda.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.constant.MessageConstant;
import com.youdianda.dto.YddAdvimgDTO;
import com.youdianda.dto.YddAdvimgPageQueryDTO;
import com.youdianda.dto.YddAdvposDTO;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;
import com.youdianda.mapper.YddAdvimgMapper;
import com.youdianda.mapper.YddAdvposMapper;
import com.youdianda.result.Result;
import com.youdianda.service.IYddAdvimgService;

import com.youdianda.vo.YddAdvimgPageQueryVO;
import com.youdianda.vo.YddAdvimgVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mosen
 * @since 2023-05-15
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements IYddAdvimgService {

    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    @Autowired
    private YddAdvposMapper yddAdvposMapper;

    /**
     * 查询广告图详情
     *
     * @param id
     * @return
     */
    @Override
    public Result selectById(Integer id) {
        //1.非空判断
        if (id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //2.业务判断
        YddAdvimg yddAdvimg = yddAdvimgMapper.selectById(id);

        //3.响应数据
        return Result.success(yddAdvimg);
    }

    /**
     * 广告图分页
     *
     * @param yddAdvimgDTO
     * @return
     */
    @Override
    public Result<YddAdvimgPageQueryVO> pageQuery(YddAdvimgPageQueryDTO yddAdvimgDTO) {
        //1.非空判断
        if(yddAdvimgDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.业务判断
          //2.1 根据广告图位置，关联的广告位id查询出对应的广告名称
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<YddAdvimg>();

        String advimgdesc = yddAdvimgDTO.getAdvimgdesc();
        String advimgpos = yddAdvimgDTO.getAdvimgpos();

        //进行模糊查询
        if(advimgdesc != null){
            lqw.like(YddAdvimg::getAdvimgdesc,advimgdesc);
        }
        if(advimgpos != null){
            lqw.like(YddAdvimg::getAdvimgpos,advimgpos);

        }

        //3.调用mybatisplus分页插件
        IPage<YddAdvimg> page = new Page<>(yddAdvimgDTO.getPage(),yddAdvimgDTO.getLimit());

        yddAdvimgMapper.selectPage(page,lqw);

        //通过page获取yddAdvimg对象集合
        List<YddAdvimg> records = page.getRecords();

        //创建一个新的集合用来返回data数据
        List<YddAdvimgVO> yddAdvimgVOList = new ArrayList<>();

        //遍历往每条数据添加广告名称
        for (YddAdvimg record : records) {
            //创建VO对象
           YddAdvimgVO yddAdvimgVO = new YddAdvimgVO();
           BeanUtils.copyProperties(record,yddAdvimgVO);

           //通过VO对象获取广告管理的ID
            String advimgpos1 = yddAdvimgVO.getAdvimgpos();
            Integer id = Integer.parseInt(advimgpos1);

            //通过广告管理ID获取
            YddAdvpos yddAdvpos = yddAdvposMapper.selectById(id);

            yddAdvimgVO.setAdvposname(yddAdvpos.getAdvposname());

           yddAdvimgVOList.add(yddAdvimgVO);

        }


        //4.封装数据
        YddAdvimgPageQueryVO vo = YddAdvimgPageQueryVO.builder()
                .currentPage(page.getCurrent())
                .totalPages(page.getPages())
                .count(page.getTotal())
                .pageSize(page.getSize())
                .data(yddAdvimgVOList)
                .build();



        return Result.success(vo);
    }

    /**
     * 修改广告图详情
     *
     * @param yddAdvimgDTO
     * @return
     */
    @Override
    public Result updateAdvimg(YddAdvimgDTO yddAdvimgDTO) {
        //1.非空判断
        if(yddAdvimgDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.业务判断

        YddAdvimg yddAdvimg = new YddAdvimg();
        BeanUtils.copyProperties(yddAdvimgDTO,yddAdvimg);
        yddAdvimgMapper.updateById(yddAdvimg);

        return Result.success();
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        //1.非空判断
        if(id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //2.业务逻辑
        yddAdvimgMapper.deleteById(id);


        return Result.success();
    }

    /**
     * 广告图添加
     *
     * @param yddAdvimgDTO
     * @return
     */
    @Override
    public Result insertAdvimg(YddAdvimgDTO yddAdvimgDTO) {
        //1.非空判断
        if(yddAdvimgDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //2.业务判断
        YddAdvimg yddAdvimg = new YddAdvimg();
        BeanUtils.copyProperties(yddAdvimgDTO,yddAdvimg);
        yddAdvimgMapper.insert(yddAdvimg);

        return Result.success();
    }
}
